<template>
	<view class="">
		<view class="p-30">
			<uv-input placeholder="请输入" v-model="userName" border="surround" clearable></uv-input>
		</view>
		<view class="bomPosition">
			<button type="primary" @click="setRemark">保存</button>
		</view>
		<uv-toast ref="toast"></uv-toast>
	</view>
</template>

<script>
	import TencentCloudChat from '@tencentcloud/chat';
	export default {
		data() {
			return {
				userName: '',
				UserSig: '',
				userInfo: {},
				userId: ''
			};
		},
		onLoad(res) {
			if (res.remark) {
				this.userName = res.remark
			}
			this.userId = res.id
			this.getUserInfo()
		},
		methods: {
			setRemark() {
				let that = this
				let options = {
					SDKAppID: this.$config.sdkappid // 接入时需要将0替换为您的即时通信应用的 SDKAppID
				};
				let chat = TencentCloudChat.create(options); // SDK 实例通常用 chat 表示
				// 更新好友备注
				let promise = chat.updateFriend({
					userID: this.userId,
					remark: this.userName
				});
				promise.then(imResponse=> {
					if (imResponse.code == 0) {
						that.$refs.toast.show({
							type: 'default',
							message: '修改成功',
							duration: 1500
						})
						uni.$emit('remark', this.userName)
						setTimeout(() => {
							uni.navigateBack({
								delta: 1
							})
						}, 1500)
					}
				}).catch(function(imError) {
					that.$refs.toast.show({
						type: 'default',
						message: imError,
						duration: 1500
					})
					console.warn('getFriendProfile error:', imError); // 更新失败
				});
			},

			onContactInfoEmitSubmit(item) {
				item.editSubmitHandler &&
					item.editSubmitHandler({
						item,
						contactInfoData: contactInfoData.value,
						isBothFriend: isBothFriend.value,
						isInBlackList: isInBlackList.value,
					});
			},

			// const setFriendInfoRes = await TencentImSDKPlugin.v2TIMManager 
			//     .getFriendshipManager()
			//     .setFriendInfo( 
			//         this.userId, //需要修改的用户id
			//         this.userName
			//     );
			// if (setFriendInfoRes.code === 0) {
			// 	console.log('修改成功');
			//     // 修改成功
			// }
			// 设置备注
			setUserName() {
				this.userList = []
				uni.request({
					url: `https://console.tim.qq.com/v4/profile/portrait_set?sdkappid=${this.$config.sdkappid}&identifier=${this.$config.identifier}&usersig=${this.UserSig}&random=${this.userId}&contenttype=json`,
					header: {
						'content-type': 'application/json',
					},
					method: 'POST',
					data: {
						"From_Account": this.userInfo.mobile,
						"ProfileItem": [{
							"Tag": "Tag_Profile_IM_Nick",
							"Value": this.userName
						}]
					},
					success: res => {
						let message = ''
						console.log(res, 'res');
						if (res.data.ActionStatus == 'OK') {
							this.$refs.toast.show({
								type: 'default',
								message: '修改成功',
								duration: 1500
							})
							setTimeout(() => {
								uni.navigateBack()
							}, 1500)
						} else {
							if (res.data.ErrorCode == 40006) {
								message = '服务器内部错误'
							} else if (res.data.ErrorCode == 40001) {
								message = '请求参数错误'
							} else if (res.data.ErrorCode == 40003) {
								message = '账号不存在'
							} else if (res.data.ErrorCode == 40004 || res.data.ErrorCode == 40008) {
								message = '暂无权限'
							} else if (res.data.ErrorCode == 40601) {
								message = '不可超过500字节'
							} else if (res.data.ErrorCode == 40610) {
								message = '类型不匹配'
							} else {
								message = '未知错误'
							}

							this.$refs.toast.show({
								type: 'default',
								message: message,
								duration: 1500
							})
						}
					},
					fail: err => {
						console.log(err, 'err');
					}
				})
			},
			getUserSig() {
				this.$api.sendRequest({
					url: '/api/Tencent/genUserSig',
					success: res => {
						this.UserSig = res.data.UserSig
					}
				})
			},
			// 获取用户信息
			getUserInfo() {
				this.$api.sendRequest({
					url: '/api/user/userInfo',
					success: res => {
						this.userInfo = res.data
						this.getUserSig()
					}
				})
			},
		}
	}
</script>

<style lang="scss">

</style>
